import { http } from '../utils/request.js'

// ========================================
// 🎨 游戏画布 API
// ========================================

// 记录绘画动作
export const paintAction = (data) => {
  return http.post('/game/paint-action', {
    roomId: data.roomId,
    x: data.x,
    y: data.y,
    color: data.color,
    tool: data.tool || 'brush'
  })
}

// 获取房间画布数据
export const getCanvasData = (roomId) => {
  return http.get(`/game/canvas/${roomId}`)
}

// 重置画布
export const resetCanvas = (roomId) => {
  return http.post(`/game/canvas/${roomId}/reset`)
}

// ========================================
// 📊 游戏统计 API
// ========================================

// 获取玩家统计信息
export const getPlayerStats = (userId) => {
  return http.get(`/stats/player/${userId}`)
}

// 获取排行榜
export const getLeaderboard = (params = {}) => {
  const { type = 'wins', limit = 10 } = params
  return http.get(`/stats/leaderboard?type=${type}&limit=${limit}`)
}

// 获取全局统计
export const getGlobalStats = () => {
  return http.get('/stats/global')
}

// 获取房间统计
export const getRoomStats = (roomId) => {
  return http.get(`/stats/room/${roomId}`)
}

// 获取房间排行榜
export const getRoomLeaderboard = (roomId) => {
  return http.get(`/stats/room/${roomId}/leaderboard`)
}

// ========================================
// 📊 游戏历史 API
// ========================================

// 获取游戏历史记录
export const getGameHistory = (params = {}) => {
  const { page = 1, pageSize = 20 } = params
  return http.get(`/game-history?page=${page}&pageSize=${pageSize}`)
}

// 获取特定游戏记录
export const getGameRecord = (gameId) => {
  return http.get(`/game-history/${gameId}`)
}

// 获取玩家游戏历史
export const getPlayerGameHistory = (userId, params = {}) => {
  const { page = 1, pageSize = 20 } = params
  return http.get(`/game-history/player/${userId}?page=${page}&pageSize=${pageSize}`)
}

// 获取房间游戏历史
export const getRoomGameHistory = (roomId, params = {}) => {
  const { page = 1, pageSize = 20 } = params
  return http.get(`/game-history/room/${roomId}?page=${page}&pageSize=${pageSize}`)
}

// 保存游戏记录
export const saveGameRecord = (record) => {
  return http.post('/game-history', record)
}
// ========================================
// 🎮 游戏房间 API (兼容性保留)
// ========================================

// 创建房间
export const createRoom = (data) => {
  return http.post('/rooms', {
    roomName: data.roomName,
    maxPlayers: data.maxPlayers || 4,
    gameMode: data.gameMode || 'classic'
  })
}

// 加入房间
export const joinRoom = (roomId, data) => {
  return http.post(`/rooms/${roomId}/join`, {
    username: data.username
  })
}

// 获取房间信息
export const getRoomInfo = (roomId) => {
  return http.get(`/rooms/${roomId}`)
}

// 获取房间列表
export const getRoomList = (params = {}) => {
  return http.get('/rooms', params)
}

// 离开房间
export const leaveRoom = (roomId) => {
  return http.post(`/rooms/${roomId}/leave`)
}

// 开始游戏
export const startGame = (roomId) => {
  return http.post(`/rooms/${roomId}/start`)
}

// 设置房间为准备就绪状态
export const setRoomReady = (roomId) => {
  return http.post(`/rooms/${roomId}/ready`)
}

// 结束游戏
export const endGame = (roomId) => {
  return http.post(`/rooms/${roomId}/end`)
}

// 获取游戏状态
export const getGameStatus = (roomId) => {
  return http.get(`/rooms/${roomId}/status`)
}

// 提交绘画数据 (兼容性保留)
export const submitDrawing = (roomId, data) => {
  return http.post(`/rooms/${roomId}/drawing`, {
    x: data.x,
    y: data.y,
    color: data.color,
    brushSize: data.brushSize,
    action: data.action // 'start', 'move', 'end'
  })
}

// 房间状态常量
export const ROOM_STATUS = {
  WAITING: 'waiting',
  PLAYING: 'playing',
  FINISHED: 'finished'
}

// 游戏模式常量
export const GAME_MODE = {
  CLASSIC: 'classic',
  TIME_LIMIT: 'time_limit',
  TEAM: 'team'
}

// 统一导出游戏API
export const gameAPI = {
  // 画布相关
  paintAction,
  getCanvasData,
  resetCanvas,
  
  // 统计相关
  getPlayerStats,
  getLeaderboard,
  getGlobalStats,
  getRoomStats,
  getRoomLeaderboard,
  
  // 历史相关
  getGameHistory,
  getGameRecord,
  getPlayerGameHistory,
  getRoomGameHistory,
  saveGameRecord,
  
  // 房间相关 (兼容性)
  createRoom,
  joinRoom,
  getRoomInfo,
  getRoomList,
  leaveRoom,
  setRoomReady,
  startGame,
  endGame,
  getGameStatus,
  submitDrawing,
  
  // 常量
  ROOM_STATUS,
  GAME_MODE
}
